package fr.vahren.dotastrat.model.builder;

import java.util.List;

import fr.vahren.dotastrat.game.D2SGame;
import fr.vahren.dotastrat.game.effect.D2SEffect;
import fr.vahren.dotastrat.model.D2SAbility;
import fr.vahren.dotastrat.model.D2SHeroUnit;
import fr.vahren.dotastrat.model.D2SMap;
import fr.vahren.dotastrat.model.D2SPlayer;
import fr.vahren.dotastrat.model.D2SUnit;
import fr.vahren.dotastrat.model.D2SUnitClass;
import fr.vahren.dotastrat.model.D2SZone;

public class D2SHeroBuilder {

	private static D2SGame game;

	public static D2SHeroUnit createHero(D2SUnitClass hero, D2SGame game) {
		if (D2SHeroBuilder.game == null) {
			D2SHeroBuilder.game = game;
		}
		switch (hero) {
		case ANTIMAGE:
			return buildAntimage();
		case EARTHSHAKER:
			return buildEarthShaker();
		default:
			return null;
		}

	}

	private static D2SHeroUnit buildEarthShaker() {
		D2SHeroUnit es = new D2SHeroUnit("Earthshaker",
				D2SUnitClass.EARTHSHAKER,
				D2SStatsGen.heroStats(D2SUnitClass.EARTHSHAKER), game);

		// add abilities
		// Enchanted totem
		D2SAbility echantedTotem = new D2SAbility() {

			private int stunDuration;

			@Override
			protected void levelUpEffects() {
				// TODO Auto-generated method stub

			}

			@Override
			protected void init() {
				priority = 2;
				stunDuration = 1;
				range = 1;
			}

			@Override
			public void execute(D2SPlayer player, D2SMap map,
					final D2SHeroUnit unit, List<D2SUnit> targetUnits,
					D2SZone targetZone) {
				for (D2SUnit target : targetUnits) {
					target.getEffects().add(D2SEffect.stunEffect(stunDuration));
				}

				// damage buff on next attack
				unit.getEffects().add(new D2SEffect() {

					private int effectBonus;

					@Override
					public void initEffect(int duration) {
						D2SHeroUnit hero = (D2SHeroUnit) unit;
						effectBonus = level
								* hero.getStats().getDamageWithoutBonus();
						hero.getStats().setBonusDamage(
								hero.getStats().getBonusDamage() + effectBonus);
					}

					@Override
					public void finish() {
						// remove buff
						D2SHeroUnit hero = (D2SHeroUnit) unit;
						hero.getStats().setBonusDamage(
								hero.getStats().getBonusDamage() - effectBonus);
						effectBonus = 0;
					}

					@Override
					public void applyEffect() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAttack(D2SUnit target, D2SUnit unit) {
						// remove buff
						D2SHeroUnit hero = (D2SHeroUnit) unit;
						hero.getStats().setBonusDamage(
								hero.getStats().getBonusDamage() - effectBonus);
						effectBonus = 0;
					}

					@Override
					public void onMove(D2SHeroUnit d2sHeroUnit, int startZone,
							int targetZone) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAbilityUsed(D2SHeroUnit d2sHeroUnit,
							D2SAbility ability) {
						// TODO Auto-generated method stub

					}
				});
			}

		};

		es.getAbilities().add(echantedTotem);

		return es;
	}

	private static D2SHeroUnit buildAntimage() {
		// TODO Auto-generated method stub
		return null;
	}

}
